package com.threeonemeeting.adapter;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.cache.FileListViewCache;

public class FileListAdapter extends ArrayAdapter<File> {

	public View listView = null;

	public FileListAdapter(Activity activity, List<File> directoryItemData,
			View listView) {

		super(activity, 0, directoryItemData);
		this.listView = listView;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		File fileItemData = getItem(position);

		// Inflate the views from XML
		View rowView = convertView;
		FileListViewCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_directory_list_item, null);
			viewCache = new FileListViewCache(rowView, fileItemData);
			rowView.setTag(viewCache);
		} else {
			viewCache = (FileListViewCache) rowView.getTag();
		}

		// 如果本地路径存在
		ImageView imageView = viewCache.getImageView();

		if (fileItemData.isDirectory()) {
			imageView.setImageResource(R.drawable.folder);
		} else {
			imageView.setImageResource(R.drawable.file);
		}

		// Set the text on the TextView
		TextView textDirectoryNameView = viewCache.getDirectoryTextView();
		textDirectoryNameView.setText(fileItemData.getName());

		return rowView;
	}
}
